<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="isPc ?'padding:30px': 'padding:10px'">
             <el-row :gutter="5" style="margin-bottom: 15px">
               <el-col :xs="18" :md="8">
                 <el-input v-model="query.mobile"  placeholder="手机号" ></el-input>
               </el-col>
               <el-col  :xs="6" :md="8">
                 <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
               </el-col>

             </el-row>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column v-if="isPc" prop="id" label="Id" width="55" align="center"></el-table-column>
                <el-table-column v-if="isPc" prop="tag" label="标签"></el-table-column>
                <el-table-column prop="mobile" label="手机号"></el-table-column>
                <el-table-column v-if="isPc" prop="nickname" label="微信昵称"></el-table-column>
                <el-table-column v-if="isPc" prop="pid" label="上级Id"></el-table-column>
                <el-table-column prop="money" label="资金余额"></el-table-column>
                <el-table-column v-if="isPc" prop="point" label="积分"></el-table-column>
                <el-table-column v-if="isPc" prop="remark" label="备注"></el-table-column>
                <el-table-column v-if="isPc" prop="level_txt" label="会员等级"></el-table-column>
                <el-table-column v-if="isPc" prop="create_time" width="160px" label="注册时间"></el-table-column>
                <el-table-column label="操作" :width="isPc ? '150px' : '120px'" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="showDetail(scope.$index, scope.row)"
                        >编辑</el-button>
                      <el-button
                          type="text"
                          icon="el-icon-bank-card"
                          @click="recharge(scope.row)"
                      >充值</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    :layout="isPc ? 'prev,next,pager' : 'prev,next'"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="基本信息" :visible.sync="editVisible" :width="modalWidth" :close-on-click-modal="false">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="标签">
                    <el-input v-model="form.tag"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.mobile"></el-input>
                </el-form-item>
                <el-form-item label="会员等级">
                    <el-select v-model="form.level" placeholder="请选择" class="handle-select mr10">
                        <el-option v-for="(v, k, i) in levelArr" :key="k" :label="v" :value="k"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="用户充值" :visible.sync="rechargeVisible" :width="modalWidth" :close-on-click-modal="false">
          <el-form ref="form" :model="rechargeForm" label-width="70px">
            <el-form-item label="充值金额">
              <el-input v-model="rechargeForm.amount"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
                  <el-button @click="rechargeVisible = false">取 消</el-button>
                  <el-button type="primary" @click="saveRecharge">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'order',
    data() {
        return {
            query: {
                mobile: '',
                page: 1,
                pageSize: 10
            },
            tableData: [],
            levelArr: [],
            orderDetail: [],
            editVisible: false,
            rechargeVisible: false,
            pageTotal: 0,
            form: {},
            rechargeForm: {
              id: 0,
              amount: 0
            },
            tagArr: [
                '淘宝', '线下', '闲鱼'
            ],
            idx: -1,
            id: -1,
            modalWidth: '80%',
            isPc: false
        };
    },
    created() {
        if (document.body.clientWidth > 786) {
            this.modalWidth = '40%'
            this.isPc = true
        }
        this.getData();
    },
    methods: {
        getData() {
            this.http.getUsers(this.query).then(res => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.total;
                this.levelArr = res.data.level_map;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 编辑操作
        showDetail(index, row) {
            this.editVisible = true
            this.form = row
        },
        recharge(row) {
          this.rechargeVisible = true
          this.rechargeForm.amount = 0
          this.rechargeForm.id = row.id
        },
        // 保存编辑
        saveEdit() {
            this.http.editUser(this.form.id, this.form)
            this.editVisible = false;
            this.$message.success(`修改成功`);
            this.form.level_txt = this.levelArr[this.form.level]
            this.$set(this.tableData, this.idx, this.form);
        },
        saveRecharge() {
            console.log(this.rechargeForm)
            this.http.editMoney(this.rechargeForm.id, this.rechargeForm)
            this.rechargeVisible = false;
            this.$message.success(`修改成功`);
            this.getData();
        },

        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
